package com.chenchao.collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Objects;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/12 16:18
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javacollection.ListTest.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 */
public class ListTest {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        arrayList.add("a");
        arrayList.add("c");
        arrayList.add("a");
        arrayList.add("a");
        arrayList.add("b");
        arrayList.add("c");
        arrayList.add("d");
        //listTest();
        //deleteElement();
        //deduplication(arrayList);
        objectDeduplication();
    }

    public static void listTest() {
        ArrayList arrayList = new ArrayList();
        arrayList.add("a");
        arrayList.add("b");
        arrayList.add("c");
        arrayList.add("d");
        //根据角标添加元素
        arrayList.add(4,"gg");
        //插入的角标要index<size
        //要在最后添加直接add（元素）就ok
        System.out.println(arrayList);

        //获取指定角标的元素
        System.out.println(arrayList.get(4));
        //遍历List
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i));
        }
        
    }

    /**
     * 删除list中指定元素
     */
    public static void deleteElement(){
        ArrayList arrayList = new ArrayList();
        arrayList.add("a");
        arrayList.add("b");
        arrayList.add("c");
        arrayList.add("d");

        Iterator iterator = arrayList.iterator();
        while(iterator.hasNext()){
            String s = (String) iterator.next();
            if(s.equals("b")){
                //arrayList.remove("b");
                //不允许直接修改arrayList结构
                // （并发问题）hasNext()每次都会检查
                //可以在迭代器里面删除,删除正在遍历的元素
                //iterator.remove();
                //但不支持添加操作
            }
        }
        ListIterator listIterator = arrayList.listIterator();
        while (listIterator.hasNext()){
            String next = (String) listIterator.next();
            if("b".equals(next)){
                listIterator.add("2");
                //list自带的迭代器支持迭代时增加操作
            }
            System.out.println(next);
        }
        System.out.println(arrayList);
        //倒序迭代
        while (listIterator.hasPrevious()){
            //System.out.println(listIterator.previousIndex());
            //单独使用会死循环因为它并不会移动角标
            System.out.println(listIterator.previousIndex()+""+listIterator.previous());

        }
    }

    /**
     * 去除重复元素
     * contains底层用（object的equls()地址判断）
     */
    public static void deduplication(ArrayList arrayList){
        //创建空集
        ArrayList result = new ArrayList();
        //遍历集合进行判断
        Iterator iterator = arrayList.iterator();
        while (iterator.hasNext()){
            String next = (String)iterator.next();
            if(!result.contains(next)){
                result.add(next);
            }
        }
        System.out.println(result);
    }

    /**
     *对象去重
     */
    public static void objectDeduplication(){
        ArrayList arrayList = new ArrayList();
        ArrayList result = new ArrayList();
        Student gg1 = new Student("张三",18);
        Student gg2 = new Student("张三",18);
        Student gg3 = new Student("李松",18);
        arrayList.add(gg1);
        arrayList.add(gg2);
        arrayList.add(gg3);
        Iterator iterator = arrayList.iterator();
        while (iterator.hasNext()){
            Object next = iterator.next();
            //contains底层还是调用对象的equals方法
            //student类里面重写了equals方法，现在不比地址，比的是值
            if(!result.contains(next)){
                result.add(next);
            }
        }
        System.out.println(result);
    }

}
class Student{
    String name;
    int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
